//
//  ExpertclassViewController.m
//  iBaby365v2
//
//  Created by admin on 14/12/1.
//  Copyright (c) 2014年 suplus. All rights reserved.
//
#define MaxReturnCount     10
#import "ExpertclassViewController.h"
#import "AFNetworking.h"
#import "UIAlertView+Kirinrin.h"
#import "MJExtension.h"
#import "SPExpertclass.h"
//#import "UIImageView+WebCache.h"
#import "ExpertViewCell.h"
#import "ClassMPListViewController.h"
#import "SPExpertContent.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "NetworkAccessManager.h"

@interface ExpertclassViewController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *Expertclasses;
@property (nonatomic,weak)MJRefreshHeaderView *headerExpert;
@property (nonatomic,weak)MJRefreshFooterView *footerExpert;
@property(nonatomic,assign)int firstReturnCount;
@end

@implementation ExpertclassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"进入ExpertclassViewController---");
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    header.delegate =self;
    
    [header beginRefreshing];
    self.headerExpert = header;
    
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.delegate = self;
    self.footerExpert = footer;
    
    self.tableView.rowHeight = 80;

}

-(void)dealloc
{
    [self.headerExpert free];
    [self.footerExpert free];
}
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    
    NSString *uriString = @"TeachingMultimediaCourseware.json";
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) {
        //        上拉刷新
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"key"] = self.appDelegate.accessKey;
        parameters[@"maxReturnCount"] = [NSNumber numberWithInt:MaxReturnCount];
        parameters[@"firstReturnCount"] = [NSNumber numberWithInt:self.firstReturnCount];
        NSLog(@"key====%@",parameters);

        
        [[NetworkAccessManager shareManager] GET:uriString parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
             
             int resultCode = [responseObject[@"result"]intValue];
             if (resultCode == 0) {
                 //             self.menuData = responseObject[@"data"];
                 NSArray *educationArray =[SPExpertclass objectArrayWithKeyValuesArray:responseObject[@"data"]];
                 [self.Expertclasses addObjectsFromArray:educationArray];
                 NSLog(@"self.educations===%d",self.Expertclasses.count);
                 [self.tableView reloadData];
                
             }else{
                 [UIAlertView alertWithRemoteError:resultCode];
             }
             [self.footerExpert endRefreshing];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             [self.footerExpert endRefreshing];
             NSLog(@"opernation: %@\nerror: %@", operation, error);
             [UIAlertView alertWithNetworkError];
         }];

    } else {
//        下拉刷新
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"key"] = self.appDelegate.accessKey;
        parameters[@"maxReturnCount"] = [NSNumber numberWithInt:MaxReturnCount];
        parameters[@"firstReturnCount"] = @0;
        NSLog(@"key====%@",parameters);
        
        
        [[NetworkAccessManager shareManager] GET:uriString parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
             
             int resultCode = [responseObject[@"result"]intValue];
             if (resultCode == 0) {
                 //             self.menuData = responseObject[@"data"];
                 NSArray *ExpertclassesAray = [SPExpertclass objectArrayWithKeyValuesArray:responseObject[@"data"]];
                 
                 self.Expertclasses =[NSMutableArray arrayWithArray:ExpertclassesAray];

                 NSLog(@"self.educations===%d",self.Expertclasses.count);
                 [self.tableView reloadData];
                 self.firstReturnCount = self.Expertclasses.count;
                 
             }else{
                 [UIAlertView alertWithRemoteError:resultCode];
             }
             [self.headerExpert endRefreshing];
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             [self.headerExpert endRefreshing];
             NSLog(@"opernation: %@\nerror: %@", operation, error);
             [UIAlertView alertWithNetworkError];
         }];

    }
}

#pragma mark - 数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Expertclasses=*****==%d",self.Expertclasses.count);
    return self.Expertclasses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExpertViewCell *cell = [ExpertViewCell cellWithTableView:tableView];
    cell.expertroom = self.Expertclasses[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中这行
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    UITableViewController *desccol = [[EdudescController alloc] init];
    [self performSegueWithIdentifier:@"classTomplist" sender:nil];
    // 取消选中这行
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id vc = segue.destinationViewController;
    if ([vc isKindOfClass:[ClassMPListViewController class]]){
        ClassMPListViewController *ccl = vc;
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        SPExpertclass *spclass = self.Expertclasses[path.row];
        ccl.spexperclass = spclass;
        NSMutableArray *contentArrays = [NSMutableArray array];
        for (NSDictionary *dict in spclass.content) {
            SPExpertContent *contentmo = [SPExpertContent contentWithDict:dict];
            [contentArrays addObject:contentmo];
        }
        ccl.spexpercontents =contentArrays;

    }
}
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

@end
